<template>
  <el-dialog class="audit_dialog" width="500px" :title="dialogTitle" :visible.sync="dialogVisible" :close-on-click-modal="false">
    <p v-if="dialogType != 3" class="text-center tip">{{ dialogType == 1 ? "您确定要拒绝审核该商品吗？" : "是否确认下列商品批量通过审核？" }}</p>
    <template v-if="dialogType != 2">
      <el-form ref="ruleForm" class="ruleForm" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="原因" prop="reason">
          <el-select v-model="form.reason" placeholder="请选择">
            <el-option label="图片不符合规范" value="图片不符合规范" />
            <el-option label="商品内容有敏感字符" value="商品内容有敏感字符" />
            <el-option label="其他原因" :value="3" />
          </el-select>
        </el-form-item>
        <el-form-item v-if="form.reason == 3" label="其他原因">
          <el-input v-model="form.other" type="textarea" :rows="4" placeholder="请输入备注原因信息！例如：图片不符合规范" />
        </el-form-item>
      </el-form>
    </template>
    <p v-if="dialogType == 3" class="text-center tip">是否确认下列商品批量拒绝审核？</p>
    <div v-if="goodsList.length > 0" class="goods_list">
      <div v-for="item in goodsList" :key="item.id">
        <div class="flex align-center goods_info">
          <el-image style="width: 60px; height: 60px" :src="item.imgurl ? $globalObj.imgBaseUrl + item.imgurl : ''">
            <div slot="error" class="image-slot">
              <i class="el-icon-picture-outline" />
            </div>
          </el-image>
          <div class="flex-1 text-left info">
            <p>{{ item.id }}</p>
            <p>{{ item.title }}</p>
          </div>
        </div>
      </div>
    </div>
    <p v-if="goodsList.length > 0" class="text-center color-999 tip" style="margin-bottom:0">共{{ goodsList.length }}个商品
    </p>
    <div slot="footer" class="dialog-footer">
      <el-button size="small" @click="dialogVisible = false">取 消</el-button>
      <el-button size="small" type="primary" @click="submit">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { goodsCheckPass, goodsCheckRefuse, goodsShelfCheckPass, goodsShelfCheckRefuse } from '@/api/audit'
export default {
  data() {
    return {
      form: {
        reason: '',
        other: ''
      },
      rules: {
        reason: [
          { required: true, message: '请选择原因', trigger: 'change' }
        ]
      },
      dialogVisible: false,
      goodsId: null,
      goodsList: [],
      listType: 1,
      dialogType: 1 // 1：拒绝审核，2：批量通过审核，3：批量拒绝审核
    }
  },
  computed: {
    dialogTitle() {
      if (this.dialogType === 1) {
        return '拒绝审核'
      } else if (this.dialogType === 2) {
        return '批量通过审核'
      } else {
        return '批量拒绝审核'
      }
    }
  },
  watch: {
    dialogVisible(val) {
      if (!val) {
        this.goodsId = null
        this.goodsList = []
      } else {
        this.listType = this.$route.name === 'auditOffGoods' ? 2 : 1
      }
    }
  },
  methods: {
    submit() {
      this.$refs['ruleForm'].validate((valid) => {
        if (valid) {
          if (this.dialogType === 1) {
            //拒绝审核'
            const params = {
              goods_id: this.goodsId,
              check_reason: ''
            }
            if (this.form.reason === 3) {
              params.check_reason = this.form.other
            } else {
              params.check_reason = this.form.reason
            }
            if (this.listType === 1) {
              goodsCheckRefuse(params).then(res => {
                this.dialogVisible = false
                this.$emit('getList', true)
              })
            } else {
              goodsShelfCheckRefuse(params).then(res => {
                this.dialogVisible = false
                this.$emit('getList', true)
              })
            }
          } else if (this.dialogType === 2) {
            //批量通过审核'
            const ids = this.goodsList.map(k => k.id).join(',')
            if (this.listType === 1) {
              goodsCheckPass(ids).then(res => {
                this.dialogVisible = false
                this.$emit('getList', true)
              })
            } else {
              goodsShelfCheckPass(ids).then(res => {
                this.dialogVisible = false
                this.$emit('getList', true)
              })
            }
          } else {
            //批量拒绝审核'
            const params = {
              goods_id: this.goodsList.map(k => k.id).join(','),
              check_reason: ''
            }
            if (this.form.reason === 3) {
              params.check_reason = this.form.other
            } else {
              params.check_reason = this.form.reason
            }
            if (this.listType === 1) {
              goodsCheckRefuse(params).then(res => {
                this.dialogVisible = false
                this.$emit('getList', true)
              })
            } else {
              goodsShelfCheckRefuse(params).then(res => {
                this.dialogVisible = false
                this.$emit('getList', true)
              })
            }
          }
        } else {
          this.$message({
            message: '请检查表单错误',
            type: 'error'
          });
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.tip {
  line-height: 35px;
  margin-bottom: 10px;
}

.goods_list {
  margin: 0 10px;
  border: 1px solid #e6e6e6;
  border-radius: 3px;
  max-height: 250px;
  overflow: auto;

  &>div {
    padding: 10px;
  }

  &>div+div {
    border-top: 1px solid #e6e6e6;
  }
}

.goods_info {
  .info {
    margin-left: 10px;

    p {
      line-height: 20px;
      color: #666666;

      &:first-child {
        color: #999999;
      }
    }
  }
}
</style>
